<?php
/**
 * Created by PhpStorm.
 * User: 宇
 * Date: 2016/9/23
 * Time: 14:46
 * 货物类
 */

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Request;
use app\index\model\Content;
use think\Session;
use app\common\controller\Index;

class Goods extends Controller
{
    public function __construct(Request $request)
    {
        parent::__construct($request);
    }
    /*
     * 商品列表
     * @param request
     * $return arr
     */
    public function goodsList()
    {
        $list = Db::name('Content')
            ->alias('c')
            ->join('hits h','c.Id = h.goods_id')
            ->join('positions_data pd', 'c.pos_id = pd.pos_id', 'LEFT')
            ->where([ 'del' => 0 ])
            ->order('update_time DESC')
            ->paginate(10);
        /*dump($list);
        exit();*/
        //获取推荐位列表
        $pos_list = model('PositionsData') ->where(['status'=>1])->select();
        $pos_arr = array();
        foreach ($pos_list as $k => $v){
            $pos_arr[$k] = $v -> toArray();
        }
        $this->assign('pos_list', $pos_arr);
//        dump($list);
//        exit();
        $this->assign('list',$list);
        $this->view->replace([
            '__PUBLIC__'    =>  '../../static/',
            '__ROOT__'      =>  '/daogou/public/'
        ]);
        return $this->fetch('goodsList');
    }

    /*
     * 添加商品
     * @param requers
     * @return bool
     */
    public function goodsAdd(Request $request)
    {
        if (!(Session::has('username'))){
            return 10005;
        } else {
            if (input('?post.dosubmit')) {
                $data_list['title'] = $request -> post('title');
                $data_list['url'] = $request -> post('url');
                $data_list['old_price'] = $request -> post('old_price');
                $data_list['new_price'] = $request -> post('new_price');
                $data_list['ticket_url'] = $request -> post('ticket_url');
                $data_list['pictrue_url'] = $request -> post('pictrue_url');
                $data_list['description'] = $request -> post('description');

                $data = array();
                /*dump($data_list);
                exit();*/
                foreach($data_list as $k => $v){
                    if (empty($v)){
                        return 10002;
                    }
                    $data[$k] = Index::validateInput($v);
                }
                //插入数据库
                //$excute = model('index/Content') ->save($data);
                $content_model = new Content();
                $excute_obj = Content::create($data);
                $excute_arr = $excute_obj -> toArray();
                $id = $excute_arr['Id'];
                //向hits表中形成记录
                $data_hits['goods_id'] = $id;
                $add_hits_re = model('Hits') ->save($data_hits);

                if (isset($excute_arr['id'])) {
                    return 10003;
                } else {
                    $this->success('提交成功!','admin/Goods/goodsList');
                }
            } else {
                $this->view->replace([
                            '__PUBLIC__'    =>  '../../static/',
                            '__ROOT__'      =>  '/daogou/public/'
                        ]);
                return $this->fetch('goodsAdd');
            }
        }
        return 0;
    }
    /*
     * 编辑商品
     * @param requers
     * @return bool
     */
    public function edit(Request $request)
    {
        if (!(Session::has('username'))){
            return 10005;
        } else {
            $id = $request->get('id');
            if (input('?post.dosubmit')) {
                $data_list['title'] = $request -> post('title');
                $data_list['url'] = $request -> post('url');
                $data_list['old_price'] = $request -> post('old_price');
                $data_list['new_price'] = $request -> post('new_price');
                $data_list['ticket_url'] = $request -> post('ticket_url');
                $data_list['pictrue_url'] = $request -> post('pictrue_url');
                $data_list['description'] = $request -> post('description');

                $data = array();
                foreach ($data_list as $k => $v) {
                    if (empty($v)) {
                        return 10002;
                    }
                    $data[$k] = Index::validateInput($v);
                }
                $data['update_time'] = time();
                //插入数据库
                $where = ['id' => $id];
                $excute = Content::where($where)->update($data);
                if (0 == $excute) {
                    return 10003;
                } else {
                    $this->success('更新成功', 'admin/Goods/goodsList');
                }
            }
            //获取该商品信息
            $goods_obj = model('index/Content')->get(['id' => $id]);
            $goods_arr = $goods_obj->toArray();
            $this->assign('goods', $goods_arr);
            $this->view->replace([
                '__PUBLIC__'    =>  '../../static/',
                '__ROOT__'      =>  '/daogou/public/'
            ]);
            return $this->fetch('goodsEdit');
        }
    }
    /*
     * 删除商品信息
     * @param requerst
     * @return bool
     */
    public function del(Request $request)
    {
        if (!(Session::has('username'))){
            return 10005;
        } else {
            $id = $request->get('id');
            if (empty($id)) {
                return 10006;
            }
            $data['del'] = -1;
            $excute = model('index/Content')->where(['id' => $id])->update($data);
            if (0 == $excute) {
                $this->error('删除失败');
            } else {
                $this->success('删除成功');
            }
        }
        return '0';
    }
    /*
     * ajax商品链接点击+1
     * @param ajax
     */
    public function goods_hits(Request $request){
        $id_input = $request -> post('id');
        $id = Index::validateInput($id_input);
        //商品链接点击+1
        Db::name('hits') -> where('goods_id', $id) -> setInc('goods_hit',1);
        //dump($excute);
    }

    /*
     * ajax优惠券链接点击+1
     * @param ajax
     */
    public function ticket_hits(Request $request){
        $id_input = $request -> post('id');
        $id = Index::validateInput($id_input);
        //商品链接点击+1
        Db::name('hits') -> where('goods_id', $id) -> setInc('ticket_hit',1);
        //dump($excute);
    }
}

